﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Drawing;

namespace LIB
{
    /// <summary>
    ///IO 的摘要说明
    /// </summary>
    public class IO
    {
        public IO()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 把文件保存
        /// </summary>
        /// <param name="filePath">物理路径</param>
        /// <param name="file">上传的文件</param>
        public static void saveFile(string filePath,HttpPostedFile file)
        {
            file.SaveAs(filePath);
            //saveFileAndThume(filePath, file, 300, 300);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="file"></param>
        /// <param name="ww"></param>
        /// <param name="hh"></param>
        /// <param name="fileNamePrefix">文件名前缀，程序会根据此前缀获取缩略图路径</param>
        public static void saveFileAndThume(string filePath, HttpPostedFile file, int ww,int hh,string fileNamePrefix)
        {
            file.SaveAs(filePath);//保存源图

            if (fileNamePrefix == "") fileNamePrefix = "headPic_";
            filePath = filePath.Replace(fileNamePrefix, "thumb//" + fileNamePrefix);
            bool _IsThumb = true;
            int OldFileHeight, OldFileWidth;
            System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream);
            OldFileHeight = img.Height;
            OldFileWidth = img.Width;
            if (_IsThumb == true)
            {
                int NewImgHeight, NewImgWidth;

                if (OldFileWidth > OldFileHeight)
                {
                    NewImgWidth = ww;
                    NewImgHeight = (int)(OldFileHeight / (float)OldFileWidth * ww);
                }
                else
                {
                    NewImgHeight = hh;
                    NewImgWidth = (int)(OldFileWidth / (float)OldFileHeight * ww);
                }
                //如果缩略图的尺寸大于原图尺寸，则用原图片大小
                if (NewImgHeight >= OldFileHeight && NewImgWidth >= OldFileWidth)
                {
                    NewImgHeight = OldFileHeight;
                    NewImgWidth = OldFileWidth;
                }

                //新建一个bmp图片  
                System.Drawing.Image NewImg = new System.Drawing.Bitmap(NewImgWidth, NewImgHeight);
                //新建一个画板  
                System.Drawing.Graphics g3 = System.Drawing.Graphics.FromImage(NewImg);
                //设置高质量插值法  
                g3.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                ////设置高质量,低速度呈现平滑程度  
                g3.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                ////清空一下画布  
                g3.Clear(Color.Blue);
                //在指定位置画图  
                g3.DrawImage(img, new System.Drawing.Rectangle(0, 0, NewImgWidth, NewImgHeight), 0, 0, img.Width, img.Height, System.Drawing.GraphicsUnit.Pixel);
                g3.Dispose();

                //System.Drawing.Image NewImg = img.GetThumbnailImage(NewImgWidth, NewImgHeight, null, System.IntPtr.Zero);
                //NewImg.Save(_strFileFolder + "Thumb-" + strFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                NewImg.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);

                ////返回数据
                //_NewImgHeight = NewImgHeight;                 //缩略图高度
                //_NewImgWidth = NewImgWidth;                  //缩略图宽度
                //// _thumbImgPath = _strNewFileFolder + "Thumb-" + strFileName;  //缩略图地址
                //_thumbImgPath = _strNewFileFolder + "Thumb\\" + _webuserid + "-" + strFileName;  //缩略图地址
            }
 
        }
    }
}